package fr.esiag.projet17.service.utilisateur;

import java.rmi.RemoteException;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;

import fr.esiag.projet17.persistence.dao.oracle.DAOFactory;
import fr.esiag.projet17.persistence.dao.oracle.impl.UtilisateurDAO;
import fr.esiag.projet17.service.IDAOService;
import fr.esiag.projet17.services.transfert.CredentialsElement;

public class UtilisateurService implements IUtilisateurService{

	private final DAOFactory daoFactory;
	
	public UtilisateurService(DAOFactory daoFactory) {
		this.daoFactory = daoFactory;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public byte[] read(byte[] idRepresentation) throws RemoteException {
		
		byte[] xml = null;
		UtilisateurWrapper uw = new UtilisateurWrapper();
		
		try {
			Long id = uw.getKeyElement(idRepresentation);
			UtilisateurDAO userDAO = daoFactory.getUtilisateurDAO();			
			xml = uw.toBytes(userDAO.read(id));
			
		} catch (JAXBException e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(),e.getCause());
		} catch (XMLStreamException e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(),e.getCause());
		}
		return xml;
	}

	@Override
	public byte[] create(byte[] newInstance) throws RemoteException {
		UtilisateurDAO userDao = daoFactory.getUtilisateurDAO();
		return null;
	}

	@Override
	public void update(byte[] transientObject) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(byte[] persistentObject) throws RemoteException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<byte[]> select() throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public byte[] readByCredentials(byte[] credentials) throws RemoteException {
		byte[] xml = null;
		UtilisateurWrapper uw = new UtilisateurWrapper();
		
		try {
			CredentialsElement credentialsElement = uw.getCredentialsElement(credentials);
			UtilisateurDAO userDAO = daoFactory.getUtilisateurDAO();			
			xml = uw.toBytes(userDAO.read(credentialsElement.getLogin(),credentialsElement.getPassword()));
			
		} catch (JAXBException e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(),e.getCause());
		} catch (XMLStreamException e) {
			e.printStackTrace();
			throw new RemoteException(e.getMessage(),e.getCause());
		}
		return xml;
	}

}
